package com.sharp.authorization.controller;

import com.sharp.authorization.annotation.auth.Auth;
import com.sharp.authorization.annotation.auth.NoneAuthRole;
import com.sharp.authorization.constants.common.RequestConst;
import com.sharp.authorization.service.docking.dto.SignDTO;
import com.sharp.authorization.service.login.LoginService;
import com.sharp.authorization.utils.token.AuthTokenUtil;
import com.sharp.authorization.vo.login.LoginRes;
import com.sharp.authorization.vo.login.SsoLoginReq;
import com.sharp.authorization.vo.platform.OpenPlatformAccessTokenResp;
import com.sharp.authorization.vo.platform.OpenPlatformRegResp;
import com.sharp.kernel.config.response.Response;
import com.sharp.kernel.config.response.ResponseHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Title: OpenPlatformController
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2019/10/9 10:17
 * @description Project Name: Grote
 * @Package: com.srct.service.account.controller
 */
@Auth
@Api(value = "公共平台相关接口", tags = "公共平台相关接口")
@RestController("OpenPlatformController")
@RequestMapping(value = "/platform")
public class OpenPlatformController {

    @Resource
    private HttpServletRequest request;

    @Resource
    private LoginService loginService;


    @ApiOperation(value = "获取接入AccessToken", notes = "获取接入AccessToken")
    @RequestMapping(value = "/accessToken", method = RequestMethod.GET)
    public ResponseEntity<Response<OpenPlatformAccessTokenResp>> fetchToken(SignDTO req) {
        Map<String, String> authMap = AuthTokenUtil.decodeAuthorization(request);
        String appKey = authMap.get(RequestConst.APP_KEY);
        String appSecret = authMap.get(RequestConst.APP_SECRET);
        OpenPlatformAccessTokenResp res = loginService.getOpenPlatformAccessToken(appKey, appSecret);
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "新增接入id", notes = "获取接入AccessToken")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseEntity<Response<OpenPlatformRegResp>> register(String name) {
        OpenPlatformRegResp res = loginService.registerOpenPlatform(name);
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "获取接入秘钥", notes = "获取接入秘钥")
    @RequestMapping(value = "/secret", method = RequestMethod.GET)
    public ResponseEntity<Response<OpenPlatformRegResp>> getAppSecret() {
        Map<String, String> authMap = AuthTokenUtil.decodeAuthorization(request);
        String appKey = authMap.get(RequestConst.APP_KEY);
        OpenPlatformRegResp res = loginService.getOpenPlatformAppSecret(appKey);
        return ResponseHandler.generateResponse(res);
    }

    @Auth(NoneAuthRole.class)
    @ApiOperation(value = "合作方单点登录", notes = "合作方单点登录")
    @RequestMapping(value = "/login/sso", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "userName", value = "用户名", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "appId", value = "三方接入id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "timeStamp", value = "时间戳", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "nonce", value = "随机码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "signature", value = "签名", required = true)})
    public ResponseEntity<Response<LoginRes>> ssoLoginOrReg(SsoLoginReq ssoLoginReq) {
        LoginRes res = loginService.ssoLoginOrReg(ssoLoginReq);
        return ResponseHandler.generateResponse(res);
    }
}
